package com.example.spring_forum.common;

import com.fasterxml.jackson.annotation.JsonInclude;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

@ApiModel("统一返回对象")
public class AppResult<T> {
    @ApiModelProperty("状态码")
    @JsonInclude(JsonInclude.Include.ALWAYS) //任何情况下都参与Json序列化
    private long code;
    @ApiModelProperty("描述信息")
    @JsonInclude(JsonInclude.Include.ALWAYS)
    private String message;
    @ApiModelProperty("返回的对象")
    @JsonInclude(JsonInclude.Include.ALWAYS)
    private T data;


    public AppResult() {
    }

    public AppResult(long code, String message) {
        this.code = code;
        this.message = message;
    }

    public AppResult(long code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    /**
     * 成功
     * @return 成功结果
     */
    public static AppResult success(){

        return new AppResult(ResultCode.SUCCESS.getCode(),ResultCode.SUCCESS.getMessage());
    }

    public static AppResult success(String message){
        return new AppResult(ResultCode.SUCCESS.getCode(), message);
    }

    public static <T>AppResult <T>success(String message,T data){
        return new AppResult<T>(ResultCode.SUCCESS.getCode(), message,data);

    }
    public static <T>AppResult <T>success(T data){
         return new AppResult<T>(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage(),data);
    }

    /**
     * 失败
     * @return 失败结果
     */
    public  static AppResult failed(){

        return new AppResult(ResultCode.FAILED.getCode(),ResultCode.FAILED.getMessage());
    }

    public  static AppResult failed(String message){

        return new AppResult(ResultCode.FAILED.getCode(),message);
    }

    public static AppResult failed(ResultCode resultCode){

        return new AppResult(ResultCode.FAILED.getCode(),resultCode.getMessage());
    }

    public long getCode() {
        return code;
    }

    public void setCode(long code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
